package yxy.game.pm2.module.passbarrier;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.apple.util.ActiveUtil;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.passbariestepreward;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.RankModule;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.timeline.JobHandle;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.Collection;
import java.util.Map;

//定时任务
public class PassbarrierJob extends JobHandle {
    protected static Logger logger = Logger.create(PassbarrierJob.class);

    @Override
    public void init() {
        handle();
    }//初始执行一次

    @Override
    public void tick() {
        handle();
    }//后续定时或间隔执行

    @Override
    public int jobType() {
        return JobHandle.JOB_TYPE_CROND;
    }//定时

    @Override
    public long period() {
        return 0;
    }

    @Override
    public String crond() {
        return "0 0 5 * * ?";
    }//每天5点刷新

    private void handle() {
        try {
            logger.info("PassbarrierJob : " + DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));

            //跨服
            Collection<AreaGroupBean> areaGroupBeans = AreaGroupBean.all();
            DateTime now = DateTime.now();

            for (AreaGroupBean groupBean : areaGroupBeans) {
                update(groupBean, now);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void update(AreaGroupBean groupBean, DateTime now) {
        try {
            //todo 清当天榜
            PassbarrierRank currentDayRank = PassbarrierRank.getRank(groupBean, PassbarrierRank.currentDayScore);
            currentDayRank.clear();
            DateTime weekBegin = ActiveUtil.getWeekBegin(now);
            if (ActiveUtil.isSameDay(weekBegin, now)) {
                //todo 发奖励，并清榜(暂时不发)
                RankModule totalScoreRank = PassbarrierRank.getRank(groupBean, PassbarrierRank.totalScore);
//                for (String playerCode : totalScoreRank.allKeys()) {
//                    RankEntry entry = totalScoreRank.get(playerCode);
//                    int rank = totalScoreRank.rank(playerCode);//排行
//                    long score = entry.getValue();//分数
//                    Map<String, passbariestepreward> allData = DataCenter.getAllData(passbariestepreward.class);
//
//                    for (passbariestepreward value : allData.values()) {
//                        if (value.getType() != 2) continue;
//
//                        if (rank >= value.getMinrank() && rank <= value.getMaxrank()) {
//                            Currencies rewards = new Currencies(Currency.create(value.getRewardtype(), value.getRewardid(), value.getRewardnum())); //奖励
//                            String title = GameString.斩关夺隘排名奖励_title().getText();
//                            String content = GameString.斩关夺隘排名奖励_msg(score, rank).getText();
//                            //邮件发送奖励
//                            new MailHandle().sendMail(playerCode, MailTypeEnum.Mail, title, content, CCReason.斩关夺隘, rewards);
//                            break;
//                        }
//                    }
//                }
                //清榜
                totalScoreRank.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
